
var util = require('../../utils/util.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    dietId: '',
    starttime: '',
    countday: '',
  },
  // 转发
  onShareAppMessage: function () {
    return {
      title: '爱丁生活方式管理营',
    }
  },


  // 转发
  onShareAppMessage: function () {
    return {
      title: '爱丁生活方式管理营',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    // 调用函数时，传入new Date()参数，返回值是日期和时间  
    var day = util.formatday(new Date());
    // 再通过setData更改Page()里面的data，动态更新页面的数据  
    this.setData({
      starttime: day
    });  

    this.setData({
      type: options.type,
      id: options.id,
      dietId: options.dietId,
      times: options.times,
      sign: options.sign,
    })
    //跳转赋值
    if (options.starttime != null && options.starttime != undefined) {
      this.setData({
        starttime: options.starttime,
        countday: options.countday,
      })
    }

    if (this.data.type == 9) {
      wx.setNavigationBarTitle({
        title: '月经打卡'
      })
    }
   
  },



  //获取时间
  bindForm: function (e) {
    var key = e.currentTarget.dataset.key,
      value = e.detail.value;
    var data = {};
    data[key] = value;
    this.setData({ starttime: value });
  },


  //点击时间获取时间和天数
  bindFormcount: function (e) {
    this.setData({
      countday: e.detail.value,
    });
  },


  addmenstap: function () {
    var that = this;
    addmenstap(that)

  },
  idsubmitadd: function () {
    var that = this;
    idsubmitadd(that)
  },

  //取消操作
  //取消操作返回上一层
  cenceltem: function () {
    wx.navigateBack({
      delta: 1
    })
  }




})



// 点击添加执行函数
function addmenstap(that) {
  
  var patientid = wx.getStorageSync('userInfo').patientid;


  if (patientid == undefined || patientid == null) {
    wx.login({
      success: function (res) {
        var code = res.code;
        wx.getUserInfo({
          success: function (res) {
            var userinfo = res.userInfo;
            app.globalData.userInfo = res.userInfo;
            that.setData({ 'userInfo': userinfo });
            var params = {
              wxcode: code,
              username: userinfo.nickName,
              icon: userinfo.avatarUrl,
              city: userinfo.province
            };
            app.https.post(that, '/IbabyWebService/3/Patient/wxmpSignUp', params, function (res) {
              wx.setStorage({
                key: "userInfo",
                data: res
              })

              //异步缓存

              wx.getStorage({
                key: 'userInfo',
                success: function (res) {
                  var patientid = res.data.patientid;
                  var out = {
                    starttime: that.data.starttime,
                    duration: that.data.countday
                  }

                  var detail = JSON.stringify(out);

                  var img = that.data.outimg;
                  var params = {
                    patientid: patientid,
                    img: img || null,
                    detail: detail,
                    type: that.data.type,
                    times: that.data.times

                  }

                  // console.info(that.data.starttime + '-----' + that.data.countday);

                  if (that.data.starttime != '' && that.data.countday != '' && that.data.starttime != undefined && that.data.countday != undefined) {

                    app.https.post(that, '/IbabyWebService/camp/submitDiet', params, function (res) {

                      var dietId = res.dietId
                      that.setData({ dietId: dietId })
                      console.log(that.data.dietId);

                      wx.redirectTo({
                        url: '../addcomplete/addcomplete?type=9&dietId=' + dietId + '&times=' + that.data.times + '&sign=' + that.data.sign
                      })
                    })

                  } else {
                    wx.showModal({
                      title: '提示',
                      content: '请填写完整!',

                    })
                  }
                 
                },

              })

            })
          },
          fail: function () {
            // fail
            console.log('获取用户信息失败')
          }
        })
      },
      fail: function () {
        console.log('授权失败')
      },
    })

  }else{
    var out = {
    starttime: that.data.starttime,
    duration: that.data.countday
  }

  var detail = JSON.stringify(out);

  var img = that.data.outimg;
  var params = {
    patientid: patientid,
    img: img || null,
    detail: detail,
    type: that.data.type,
    times: that.data.times

  }

  // console.info(that.data.starttime + '-----' + that.data.countday);

  if (that.data.starttime != '' && that.data.countday != '' && that.data.starttime != undefined && that.data.countday != undefined) {

    app.https.post(that, '/IbabyWebService/camp/submitDiet', params, function (res) {

      var dietId = res.dietId
      that.setData({ dietId: dietId })
      console.log(that.data.dietId);

      wx.redirectTo({
        url: '../addcomplete/addcomplete?type=9&dietId=' + dietId + '&times=' + that.data.times + '&sign=' + that.data.sign
      })
    })

  } else {
    wx.showModal({
      title: '提示',
      content: '请填写完整!',

    })
  }
  }
 
  

}

//进行添加后修改操作
function idsubmitadd(that) {

  var out = {
    starttime: that.data.starttime,
    duration: that.data.countday
  }
  var detail = JSON.stringify(out);

  var paramsadd = {
    detail: detail,
    id: that.data.dietId
  }

  console.log(that.data.dietId);

  if (that.data.starttime != '' && that.data.countday != '' && that.data.starttime != undefined && that.data.countday != undefined) {

  app.https.post(that, '/IbabyWebService/camp/clockEdit', paramsadd, function (res) {

    console.log(res);
    wx.redirectTo({
      url: '../addcomplete/addcomplete?type=9&dietId=' + that.data.dietId
    })
  })

  } else {
    wx.showModal({
      title: '提示',
      content: '请填写完整!',

    })
  }


}